package com.content.view.Frame.MainPanel.MPMyShelf
{
	
	import com.clerkq.Tabs.TabManager;
	import com.clerkq.Tabs.events.TabChangeEvent;
	import com.content.model.Collection.ItemGroup;
	import com.content.model.OnlineRes.OnlineResType;
	import com.content.model.OnlineRes.OnlineResVO;
	import com.content.model.OnlineRes.OnlineResVO_Audio;
	import com.content.model.OnlineRes.OnlineResVO_Book;
	import com.content.model.OnlineRes.OnlineResVO_Video;
	import com.content.view.Component.OnlineRes.OnlineResEvent;
	import com.content.view.Component.OnlineRes.ResItem;
	import com.content.view.Component.PurchasedRes.SPMyShelf;
	import com.content.view.Frame.MainPanel.MPBase;
	import com.content.view.Share.Player.AudioPlayer;
	import com.content.view.Share.Player.AudioPlayer_Mediator;
	import com.content.view.Share.Player.BookPlayer;
	import com.content.view.Share.Player.BookPlayer_Mediator;
	import com.content.view.Share.Player.VideoPlayer;
	import com.content.view.Share.Player.VideoPlayer_Mediator;
	import com.library.utils.TimeUtil;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.DataEvent;
	import flash.events.MouseEvent;
	
	import gs.TweenLite;
	import gs.easing.Cubic;
	
	public final class MPMyShelf extends MPBase
	{
		public static const CHOOSE_RESOURCE:String = "MPMyShelf-" + "ChooseResource";
		
		private var _spShelf:SPMyShelf;
		private var _tabManager:TabManager;
		private var _tab:MovieClip;
		
		private var _playerAudio:AudioPlayer_Mediator;
		private var _playerVideo:VideoPlayer_Mediator;
		private var _playerBook:BookPlayer_Mediator;
		
		public function MPMyShelf(	marginTop:int,
									marginBottom:int,
									marginLeft:int,
									marginRight:int,
									cBarWidth:int, 
									playerAudio:AudioPlayer_Mediator,
									playerVideo:VideoPlayer_Mediator,
									playerBook:BookPlayer_Mediator,
									bgSkin:Sprite = null)
		{
			super(	
				this,
				marginTop,
				marginBottom,
				marginLeft,
				marginRight,
				cBarWidth, 
				bgSkin);
			
			_playerAudio = playerAudio;
			_playerVideo = playerVideo;
			_playerBook = playerBook;
			
			_tab = new MovieClip();
			_container.addChild(_tab);
			_tab.graphics.beginFill(0xff0000, 1.0);
			_tab.graphics.drawRect(0,0,200, 10);
			
			_tabManager = new TabManager( _tab );
			
			_spShelf = new SPMyShelf();
			_container.addChild(_spShelf);
			
			_container.addChild( _tabManager );
			
			_tabManager.addTab( "全部");
			_tabManager.addTab( "图书");
			_tabManager.addTab( "视频");
			_tabManager.addTab( "听书");
			
			_spShelf.addEventListener(SPMyShelf.PLAY_RES, OnPlayRes);
			_tabManager.addEventListener(TabChangeEvent.TABCHANGED_EVENT, handleTabChanged);
		}
		override protected function SetLayoutMP(areaWidth:int, areaHeight:int):void{
			_spShelf.SetLayout( int(areaWidth*1.0), int(areaHeight*1.0)-30 );
			_spShelf.y = 30;
			
			_tab.x = 30;
			_tab.y = 20;
		}
		
		/* Event Handler
		* --------------------------------------------------- */
		private function OnPlayRes(evt:OnlineResEvent):void{
			switch(evt.OLResVO.Resource){
				case OnlineResType.AUDIO:
					_playerAudio.Play(evt.OLResVO as OnlineResVO_Audio);
					break;
				case OnlineResType.VIDEO:
					_playerVideo.Play(evt.OLResVO as OnlineResVO_Video);
					break;
				case OnlineResType.BOOK:
					_playerBook.Play(evt.OLResVO as OnlineResVO_Book);
					break;
			}
			//VideoPlayer.instance.Play(evt.OLResVO as OnlineResVO_Video);
		}
		private function handleTabChanged(evt:TabChangeEvent):void{
			dispatchEvent(new DataEvent(CHOOSE_RESOURCE, false, false, evt.tabName));
		}
		
		
		/* Public Method
		* ---------------------------------------------------- */
		public function FeedUpdate(xml:XML):void{
			var cmdName:String = xml.head.command;
			
			var resItem:XML;	
			var resVOGroup:ItemGroup = new ItemGroup();
			for each (resItem in xml.body.shelf.item)
			{
				var rVO:OnlineResVO = new OnlineResVO();
				rVO.Resource = resItem.@resource;
				rVO.ResID = resItem.@id;
				rVO.Name = resItem.info.(@id=="name")
				rVO.Description = resItem.info.(@id=="description")
				rVO.Author = resItem.info.(@id=="author")
				rVO.Publisher = resItem.info.(@id=="publisher")
				rVO.PublishDate = TimeUtil.DateStringToObject(resItem.info.(@id=="publishdate"));
				rVO.Star = int(resItem.info.(@id=="star"));
				rVO.Purchased = resItem.info.(@id=="purchased").toLowerCase() == "true"?true:false;
				rVO.CoverURL = resItem.info.(@id=="coverurl")
				rVO.CoverURLSmall = resItem.info.(@id=="coverurlsmall")
				rVO.DownUrl = resItem.info.(@id=="downurl")
				resVOGroup.AddVO(rVO);
			}
			_spShelf.FillResWrapper(resVOGroup);
		}
		override public function Dispose():void
		{
			_playerAudio = null;
			_playerVideo = null;
			_playerBook = null;
			
			_spShelf.addEventListener(SPMyShelf.PLAY_RES, OnPlayRes);
			_tabManager.addEventListener(TabChangeEvent.TABCHANGED_EVENT, handleTabChanged);
			_spShelf.Dispose();
		}
	}
}